package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysMarket;
import com.ruoyi.system.domain.SysMarketFile;
import com.ruoyi.system.service.ISysMarketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "市场调研")
@RequestMapping("sys/market")
@RestController
public class SysMarketController extends BaseController {
    @Autowired
    private ISysMarketService sysMarketService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询市场调研列表
     */
    @ApiOperation("查询市场调研列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:list')")
    @GetMapping("/list/{deviceId}")
    public AjaxResult list(@PathVariable("deviceId") Long deviceId)
    {
        List<SysMarket> list = sysMarketService.selectSysMarketListById(deviceId);
        return AjaxResult.success(list);
    }

    /**
     * 根据文件Id 获取对应文件
     */
    @ApiOperation(value = "根据文件Id 获取对应文件")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:listfile')")
    @GetMapping(value = "/listBy/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        List<SysMarketFile> list =sysMarketService.selectDocFileByFileId(id);
        return AjaxResult.success(list);
    }

    /**
     * 新增市场调研列表
     */
    @ApiOperation(value = "新增市场调研列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:add')")
    @Log(title = "新增市场调研列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysMarket sysMarket)
    {
        sysMarket.setPerson(getUsername());
        return toAjax(sysMarketService.insertSysMarket(sysMarket));
    }

    /**-
     * 修改市场调研列表
     */
    @ApiOperation(value = "修改市场调研列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:state')")
    @Log(title = "修改市场调研列表", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody SysMarket sysMarket)
    {
        sysMarket.setPerson(getUsername());
        return toAjax(sysMarketService.updateSysMarket(sysMarket));
    }

    /**
     * 逻辑删除文件
     */
    @ApiOperation(value = "逻辑删除文件")
    @PreAuthorize("@ss.hasPermi('lifecycle:marketResearch:delete')")
    @Log(title = "逻辑删除文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileId}")
    public AjaxResult remove(@PathVariable Long fileId) {
        return toAjax(sysMarketService.deleteMarketFileById(fileId));
    }


    /**
     * 上传请求
     */
    @ApiOperation(value = "实现文件上传")
    @Log(title = "文件上传", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file,@RequestParam("sysDeviceId") Long sysDeviceId ) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            String name = file.getOriginalFilename();
            SysMarketFile sysMarket = new SysMarketFile();
            sysMarket.setCreateBy(getUsername());
            sysMarket.setFileName(name);
            sysMarket.setFileUnique(fileName);
            sysMarket.setFilePath(filePath);
            sysMarket.setSysDeviceId(sysDeviceId);
            sysMarket.setUrl(url);
            Long fileId = sysMarketService.insertPathName(sysMarket);
            ajax.put("name", name);
            ajax.put("fileId", fileId);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
